/*
 * @Author: WangShuai 1830773767@qq.com
 * @Date: 2025-05-28 14:11:41
 * @LastEditors: WangShuai 1830773767@qq.com
 * @LastEditTime: 2025-05-31 09:46:39
 * @FilePath: \test\vite.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
    fileURLToPath,
    URL
} from "node:url";
import {
    defineConfig,
    loadEnv
} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
//注册ElementPlus组件
import {
    ElementPlusResolver
} from "unplugin-vue-components/resolvers";
// https://vitejs.dev/config/
export default defineConfig((command, model) => {
    const env = loadEnv(model, process.cwd(), "")
    // console.log("evnv",env.VITE_API_URL);
    return {
        plugins: [vue(),
            AutoImport({
                resolvers: [ElementPlusResolver(),],
            }),
            Components({
                resolvers: [ElementPlusResolver(),],
            }),
        ],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src",
                    import.meta.url)),
            },
        },
        server: {
            proxy: {
                '/api': {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => {
                        console.log('[Proxy]正在重写路径:', path)
                        return path 
                      },
                      // 启用代理请求的日志
                    configure: (proxy, options) => {
                        proxy.on('proxyReq', (proxyReq, req, res) => {
                            console.log('[Proxy] 正在发送请求到:', proxyReq.path);
                        })
                        proxy.on('proxyRes', (proxyRes, req, res) => {
                            console.log('[Proxy] 从目标地址收到响应，状态码为:', proxyRes.statusCode);
                        })
                    },
                },
                
                //访问localhost:6163拦截转发
                "/igs": {
                    target: "http://localhost:6163",
                    changeOrigin: true,
                    rewrite: (path) => {
                        console.log("path", path)
                        return path
                    },
                    configure: (proxy, options) => {
                        proxy.on('proxyReq', (proxyReq, req, res) => {
                            console.log('[Proxy] 正在发送请求到:', proxyReq.path);
                        })
                        proxy.on('proxyRes', (proxyRes, req, res) => {
                            console.log('[Proxy] 从目标地址收到响应，状态码为:', proxyRes.statusCode);
                        })
                    },
                }
            }
        }
    }
})